package com.huan.yygh.controller.api;

import com.huan.yygh.common.helper.HttpRequestHelper;
import com.huan.yygh.common.result.Result;
import com.huan.yygh.model.hosp.Department;
import com.huan.yygh.model.hosp.Hospital;
import com.huan.yygh.model.hosp.Schedule;
import com.huan.yygh.service.DepartmentServcie;
import com.huan.yygh.service.HospitalService;
import com.huan.yygh.service.HospitalSetService;
import com.huan.yygh.service.ScheduleService;
import com.huan.yygh.vo.hosp.DepartmentQueryVo;
import com.huan.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentServcie departmentServcie;
    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取信息
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        Integer page = StringUtils.isEmpty((String) paramMap.get("page"))? 1:Integer.parseInt((String) paramMap.get("page"));
        Integer limit = StringUtils.isEmpty((String) paramMap.get("limit"))? 1:Integer.parseInt((String) paramMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        Page<Schedule> pages = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pages);
    }
    //上传排班接口
    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        scheduleService.save(paramMap);
        return Result.ok();
    }


    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        departmentServcie.remove(hoscode,depcode);
        return Result.ok();
    }

    @ApiOperation(value = "获取分页列表")
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取信息
        String hoscode = (String) paramMap.get("hoscode");
        Integer page = StringUtils.isEmpty((String) paramMap.get("page"))? 1:Integer.parseInt((String) paramMap.get("page"));
        Integer limit = StringUtils.isEmpty((String) paramMap.get("limit"))? 1:Integer.parseInt((String) paramMap.get("limit"));

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        Page<Department> pages = departmentServcie.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pages);
    }

    @ApiOperation("上传部门信息")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //调用service中方法
        departmentServcie.save(paramMap);
        return Result.ok();
    }
    //查询医院
    @ApiOperation("展示医院信息")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String)paramMap.get("hoscode");
        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    //上传医院接口
    @ApiOperation("上传医院信息")
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

//        //1 获取医院系统传递过来的签名,签名进行MD5加密
//        String hospSign = (String)paramMap.get("sign");
//
//        //2 根据传递过来医院编码，查询数据库，查询签名
//        String hoscode = (String)paramMap.get("hoscode");
//        String signKey = hospitalSetService.getSignKey(hoscode);
//
//        //3 把数据库查询签名进行MD5加密
//        String signKeyMd5 = MD5.encrypt(signKey);
//
//        //4 判断签名是否一致
//        if(!hospSign.equals(signKeyMd5)) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String)paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);

        //调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();
    }
}
